/* Generate Table 1, Table 2, Table 3 */

* Author: Xiao Wang
* Version: 07/2020

cd "C:\Users\Xiao\Box\TRI_relocation_project\Paper1\Replication_kit"


* Do the cross-sectional regressions 

use data_crs_regs, clear 


* Regressions 
set matsize 3500
set more off

// Gen Linear Splines 
capture drop sp* 
_pctile l2totalrelt, p(65 75 90 95)
mkspline sp1 `r(r1)' sp2 `r(r2)' sp3 `r(r3)' sp4 `r(r4)' sp5 = l2totalrelt, di 

label var sp1 "Emis < 65th pct"
label var sp2 "Emis 65th to 75th pct"
label var sp3 "Emis 75th to 90th pct"
label var sp4 "Emis 90th to 95th pct"
label var sp5 "Emis > 95th pct"


// Independent vars
global x1 "lgemp"
global x2 "paydexdiff paydexmax"
 
global m1nb "lgpopdensity_ctnb"
global m2nb "lgpcinc_ctnb edu3_rate_ctnb"
global m3nb "whitenonhis_ratenb voterturnoutnb demo_ratenb"
global m31nb "whitenonhis_ratenb"

global z1 "NA"
global z2nb "NTRI_ctnb"
global z3nb "lgwageindcnty highwaymiles2001nb railmiles2003nb urban_rate_ctnb housingvalue_adj1990nb" 
global z31nb "highwaymiles2001nb railmiles2003nb urban_rate_ctnb housingvalue_adj1990nb" 


* ------------
* Table 1 
* ------------
set more off 
eststo crs_200: qui logit move199020 $m2nb $m3nb $m1nb $x1 $x2 i.stateID i.sic2digm, cluster(tractID) 

eststo crs_201: qui logit move199020 $m2nb $m3nb $m1nb $x1 $x2 sp* zero_tl i.stateID i.sic2digm, cluster(tractID) 

eststo crs_202: qui logit move199020 $m2nb $m3nb $m1nb $x1 $x2 sp* zero_tl $z1 $z2nb $z3nb i.sic2digm i.stateID, cluster(tractID) 

eststo cntycrs_200: qui logit move199020_cnty $m2nb $m3nb $m1nb $x1 $x2 sp* zero_tl $z1 $z2nb $z3nb i.sic2digm i.stateID, vce(cluster tractID)

eststo ctcrs_200: qui logit move199020_ct $m2nb $m31nb $m1nb $x1 $x2 sp* zero_tl $z2nb $z31nb i.sic2digm i.stateID if maxmove_cnty == 0 & lgwageindcnty ~= . & voterturnout ~= ., cluster(tractID) 

esttab crs_200 crs_201 crs_202 cntycrs_200 ctcrs_200 using Table2.csv, replace ///
	drop(*.sic2digm *.stateID) ///
	mtitle("Moved in 10 Years" "" "" "Moved in 20 years" "" "") ///
	label interaction(" x ") b(3) ///
	nogap nodep nonotes compress se ar2 star(* 0.10 ** 0.05 *** 0.01)


* ----------------------------------------------
* Do panel regressions

use data_panel_regs, clear 
xtset facilityID year
* -------------------
** Regressions 
* -------------------

* Covariates 
global x1 "lgemp"
global x2 "paydexdiff paydexmax"
 
global m1nb "lgpopdensity_ctnb1990"
global m2nb "lgpcinc_ctnb1990 edu3_rate_ctnb1990"
global m3nb "whitenonhis_ratenb1990 voterturnoutnb1990 demo_ratenb1990 "
global m31nb "whitenonhis_ratenb1990"

global z1 "NA"
global z2nb "NTRI_ctnb"
global z3nb "lgwageindcnty highwaymiles2001nb1990 railmiles2003nb1990 urban_rate_ctnb1990 housingvalue_adj1990nb1990" 
global z31nb "highwaymiles2001nb1990 railmiles2003nb1990 urban_rate_ctnb1990 housingvalue_adj1990nb1990" 

capture drop sp1 sp2 sp3 sp4 sp5 
mkspline sp1 1.5 sp2 15.36 sp3 104.64 sp4 279.24 sp5 = l2totalrelt, di 
	// same as the cross-sectional regression spline points

label var sp1 "Emis < 65th pct"
label var sp2 "Emis 65th to 75th pct"
label var sp3 "Emis 75th to 90th pct"
label var sp4 "Emis 90th to 95th pct"
label var sp5 "Emis > 95th pct"

* ---------------
* Table 2 
* ---------------
eststo pl_2: qui logit moving_ctcnty $m2nb $m3nb $m1nb l.($x1 $x2) (sp* zero_tl) l.($z1 $z2nb $z3nb) i.stateID i.sic2digm i.year, vce(cluster tractID)
margins, dydx(lgpopdensity_ctnb1990 edu3_rate_ctnb1990 l.lgwageindcnty) at((mean)_all)  

eststo pl_0: qui logit moving_ctcnty $m2nb $m3nb $m1nb l.($x1 $x2) i.stateID i.sic2digm i.year if e(sample) == 1, cluster(tractID) 

eststo pl_1: qui logit moving_ctcnty $m2nb $m3nb $m1nb l.($x1 $x2) (sp* zero_tl) i.stateID i.sic2digm i.year if e(sample) == 1, vce(cluster tractID)

eststo cntypl_0: qui logit moving_cnty $m2nb $m3nb $m1nb l.($x1 $x2) (sp* zero_tl) l.($z1 $z2nb $z3nb) i.stateID i.sic2digm i.year, cluster(tractID) 

eststo ctpl_0: qui logit moving_ct $m2nb $m31nb $m1nb l.($x1 $x2) (sp* zero_tl) l.($z2nb $z31nb) i.stateID i.sic2digm i.year if maxmove_cnty == 0 & lgwageindcnty ~= . & voterturnoutnb1990 ~= ., cluster(tractID) 

esttab pl_0 pl_1 pl_2 cntypl_0 ctpl_0 using Table2.csv, replace /// 
	drop(*.sic2digm *.stateID *.year) ///
	mtitle("All Moves" "" "" "Cross Cnty" "Within Cnty") ///
	label interaction(" x ") b(3) ar2 se star(* 0.10 ** 0.05 *** 0.01) ///
	nogap nodep nonotes compress 


* -------------------
* Table 3
* -------------------
global x3 "nopastTRI zero_tl"
gen nopastTRIpop = nopastTRI * lgpopdensity_ctnb1990
gen nopastTRIedu = nopastTRI * edu3_rate_ctnb1990
gen nopastTRIinc = nopastTRI * lgpcinc_ctnb1990

eststo t7: qui logit moving_ctcnty $x3 $m2nb $m3nb $m1nb l.($x1 $x2) (sp* zero_tl) l.($z1 $z2nb $z3nb) i.sic2digm i.year i.stateID if year >= 1995, vce(cluster tractID)
codebook facilityID if e(sample) == 1
eststo t7_pop: qui logit moving_ctcnty $x3 nopastTRIpop $m2nb $m3nb $m1nb l.($x1 $x2) (sp* zero_tl) l.($z1 $z2nb $z3nb) i.sic2digm i.year i.stateID if year >= 1995, vce(cluster tractID)
eststo t7_edu: qui logit moving_ctcnty $x3 nopastTRIedu $m2nb $m3nb $m1nb l.($x1 $x2) (sp* zero_tl) l.($z1 $z2nb $z3nb) i.sic2digm i.year i.stateID if year >= 1995, vce(cluster tractID)

eststo cntyt7: qui logit moving_cnty $x3 $m2nb $m3nb $m1nb l.($x1 $x2) (sp* zero_tl) l.($z1 $z2nb $z3nb) i.sic2digm i.year i.stateID if year >= 1995, vce(cluster tractID)
outreg2 using table7.doc, label append drop(i.sic2digm i.stateID i.year) dec(3) ctitle(Moved in year t) addtext(Industry FE, YES, State FE, YES, Year FE, YES) 
eststo cntyt7_pop: qui logit moving_cnty $x3 nopastTRIpop $m2nb $m3nb $m1nb l.($x1 $x2) (sp* zero_tl) l.($z1 $z2nb $z3nb) i.sic2digm i.year i.stateID if year >= 1995, vce(cluster tractID)
eststo cntyt7_edu: qui logit moving_cnty $x3 nopastTRIedu $m2nb $m3nb $m1nb l.($x1 $x2) (sp* zero_tl) l.($z1 $z2nb $z3nb) i.sic2digm i.year i.stateID if year >= 1995, vce(cluster tractID)
codebook facilityID if e(sample) == 1

label var nopastTRI "Past TRI Reporting (1=No)"
label var zero_tl "Current TRI Reporting (1=No)"
esttab t7 t7_pop t7_edu cntyt7 cntyt7_pop cntyt7_edu using Table3.csv, replace /// 
	drop(*.sic2digm *.stateID *.year) ///
	mtitle("All Moves" "" "" "Cross Cnty" "Within Cnty") ///
	label interaction(" x ") b(3) ar2 se star(* 0.10 ** 0.05 *** 0.01) ///
	nogap nodep nonotes noomitted compress 
		
